home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 7 / rzsz0589.zip / RBSB.C < prev    next >
C/C++ Source or Header  |  1989-05-10  |  7KB  |  328 lines

  1. /*
  2.  *
  3.  *  Rev 5-09-89
  4.  *  This file contains Unix specific code for setting terminal modes,
  5.  *  very little is specific to ZMODEM or YMODEM per se (that code is in
  6.  *  sz.c and rz.c).  The CRC-16 routines used by XMODEM, YMODEM, and ZMODEM
  7.  *  are also in this file, a fast table driven macro version
  8.  *
  9.  *    V7/BSD HACKERS:  SEE NOTES UNDER mode(2) !!!
  10.  *
  11.  *   This file is #included so the main file can set parameters such as HOWMANY.
  12.  *   See the main files (rz.c/sz.c) for compile instructions.
  13.  */
  14.  
  15. #ifdef V7
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #define STAT
  19. #include <sgtty.h>
  20. #define OS "V7/BSD"
  21. #define ROPMODE "r"
  22. #ifdef LLITOUT
  23. long Locmode;        /* Saved "local mode" for 4.x BSD "new driver" */
  24. long Locbit = LLITOUT;    /* Bit SUPPOSED to disable output translations */
  25. #include <strings.h>
  26. #endif
  27. #endif
  28.  
  29. #ifndef OS
  30. #ifndef USG
  31. #define USG
  32. #endif
  33. #endif
  34.  
  35. #ifdef USG
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #define STAT
  39. #include <termio.h>
  40. #define OS "SYS III/V"
  41. #define ROPMODE "r"
  42. #define MODE2OK
  43. #include <string.h>
  44. #endif
  45.  
  46. #if HOWMANY  > 255
  47. Howmany must be 255 or less
  48. #endif
  49.  
  50. /*
  51.  * return 1 iff stdout and stderr are different devices
  52.  *  indicating this program operating with a modem on a
  53.  *  different line
  54.  */
  55. int Fromcu;        /* Were called from cu or yam */
  56. from_cu()
  57. {
  58.     struct stat a, b;
  59.  
  60.     fstat(1, &a); fstat(2, &b);
  61.     Fromcu = a.st_rdev != b.st_rdev;
  62.     return;
  63. }
  64. cucheck()
  65. {
  66.     if (Fromcu)
  67.         fprintf(stderr,"Please read the manual page BUGS chapter!\r\n");
  68. }
  69.  
  70.  
  71. struct {
  72.     unsigned baudr;
  73.     int speedcode;
  74. } speeds[] = {
  75.     110,    B110,
  76.     300,    B300,
  77.     600,    B600,
  78.     1200,    B1200,
  79.     2400,    B2400,
  80.     4800,    B4800,
  81.     9600,    B9600,
  82.     19200,    EXTA,
  83.     38400,    EXTB,
  84.     0,
  85. };
  86.  
  87. int Twostop;        /* Use two stop bits */
  88.  
  89.  
  90. /*
  91.  *  The following uses an external rdchk() routine if available,
  92.  *  otherwise defines the function for BSD or fakes it for SYSV.
  93.  */
  94.  
  95. #ifndef READCHECK
  96. #ifdef FIONREAD
  97. #define READCHECK
  98. /*
  99.  *  Return non 0 iff something to read from io descriptor f
  100.  */
  101. rdchk(f)
  102. {
  103.     static long lf;
  104.  
  105.     ioctl(f, FIONREAD, &lf);
  106.     return ((int) lf);
  107. }
  108.  
  109. #else        /* FIONREAD */
  110.  
  111. #ifdef SV
  112. #define READCHECK
  113. #include <fcntl.h>
  114.  
  115. int checked = 0;
  116. /*
  117.  * Nonblocking I/O is a bit different in System V, Release 2
  118.  *  Note: this rdchk vsn throws away a byte, OK for ZMODEM
  119.  *  sender because protocol design anticipates this problem.
  120.  */
  121. #define EATSIT
  122. rdchk(f)
  123. {
  124.     int lf, savestat;
  125.     static char bchecked;
  126.  
  127.     savestat = fcntl(f, F_GETFL) ;
  128.     fcntl(f, F_SETFL, savestat | O_NDELAY) ;
  129.     lf = read(f, &bchecked, 1) ;
  130.     fcntl(f, F_SETFL, savestat) ;
  131.     checked = bchecked & 0377;    /* force unsigned byte */
  132.     return(lf) ;
  133. }
  134. #endif
  135. #endif
  136. #endif
  137.  
  138.  
  139. static unsigned
  140. getspeed(code)
  141. {
  142.     register n;
  143.  
  144.     for (n=0; speeds[n].baudr; ++n)
  145.         if (speeds[n].speedcode == code)
  146.             return speeds[n].baudr;
  147.     return 38400;    /* Assume fifo if ioctl failed */
  148. }
  149.  
  150.  
  151.  
  152. #ifdef ICANON
  153. struct termio oldtty, tty;
  154. #else
  155. struct sgttyb oldtty, tty;
  156. struct tchars oldtch, tch;
  157. #endif
  158.  
  159. /*
  160.  * mode(n)
  161.  *  3: save old tty stat, set raw mode with flow control
  162.  *  2: set XON/XOFF for sb/sz with ZMODEM or YMODEM-g
  163.  *  1: save old tty stat, set raw mode 
  164.  *  0: restore original tty mode
  165.  */
  166. mode(n)
  167. {
  168.     static did0 = FALSE;
  169.  
  170.     vfile("mode:%d", n);
  171.     switch(n) {
  172. #ifdef USG
  173.     case 2:        /* Un-raw mode used by sz, sb when -g detected */
  174.         if(!did0)
  175.             (void) ioctl(0, TCGETA, &oldtty);
  176.         tty = oldtty;
  177.  
  178.         tty.c_iflag = BRKINT|IXON;
  179.  
  180.         tty.c_oflag = 0;    /* Transparent output */
  181.  
  182.         tty.c_cflag &= ~PARENB;    /* Disable parity */
  183.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  184.         if (Twostop)
  185.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  186.  
  187.  
  188. #ifdef READCHECK
  189.         tty.c_lflag = Zmodem ? 0 : ISIG;
  190.         tty.c_cc[VINTR] = Zmodem ? -1:030;    /* Interrupt char */
  191. #else
  192.         tty.c_lflag = ISIG;
  193.         tty.c_cc[VINTR] = Zmodem ? 03:030;    /* Interrupt char */
  194. #endif
  195.         tty.c_cc[VQUIT] = -1;            /* Quit char */
  196. #ifdef NFGVMIN
  197.         tty.c_cc[VMIN] = 1;
  198. #else
  199.         tty.c_cc[VMIN] = 3;     /* This many chars satisfies reads */
  200. #endif
  201.         tty.c_cc[VTIME] = 1;    /* or in this many tenths of seconds */
  202.  
  203.         (void) ioctl(0, TCSETAW, &tty);
  204.         did0 = TRUE;
  205.         return OK;
  206.     case 1:
  207.     case 3:
  208.         if(!did0)
  209.             (void) ioctl(0, TCGETA, &oldtty);
  210.         tty = oldtty;
  211.  
  212.         tty.c_iflag = n==3 ? (IGNBRK|IXOFF) : IGNBRK;
  213.  
  214.          /* No echo, crlf mapping, INTR, QUIT, delays, no erase/kill */
  215.         tty.c_lflag &= ~(ECHO | ICANON | ISIG);
  216.  
  217.         tty.c_oflag = 0;    /* Transparent output */
  218.  
  219.         tty.c_cflag &= ~PARENB;    /* Same baud rate, disable parity */
  220.         tty.c_cflag |= CS8;    /* Set character size = 8 */
  221.         if (Twostop)
  222.             tty.c_cflag |= CSTOPB;    /* Set two stop bits */
  223. #ifdef NFGVMIN
  224.         tty.c_cc[VMIN] = 1; /* This many chars satisfies reads */
  225. #else
  226.         tty.c_cc[VMIN] = HOWMANY; /* This many chars satisfies reads */
  227. #endif
  228.         tty.c_cc[VTIME] = 1;    /* or in this many tenths of seconds */
  229.         (void) ioctl(0, TCSETAW, &tty);
  230.         did0 = TRUE;
  231.         Effbaud = Baudrate = getspeed(tty.c_cflag & CBAUD);
  232.         return OK;
  233. #endif
  234. #ifdef V7
  235.     /*
  236.      *  NOTE: this should transmit all 8 bits and at the same time
  237.      *   respond to XOFF/XON flow control.  If no FIONREAD or other
  238.      *   rdchk() alternative, also must respond to INTRRUPT char
  239.      *   This doesn't work with V7.  It should work with LLITOUT,
  240.      *   but LLITOUT was broken on the machine I tried it on.
  241.      */
  242.     case 2:        /* Un-raw mode used by sz, sb when -g detected */
  243.         if(!did0) {
  244.             ioctl(0, TIOCEXCL, 0);
  245.             ioctl(0, TIOCGETP, &oldtty);
  246.             ioctl(0, TIOCGETC, &oldtch);
  247. #ifdef LLITOUT
  248.             ioctl(0, TIOCLGET, &Locmode);
  249. #endif
  250.         }
  251.         tty = oldtty;
  252.         tch = oldtch;
  253. #ifdef READCHECK
  254.         tch.t_intrc = Zmodem ? -1:030;    /* Interrupt char */
  255. #else
  256.         tch.t_intrc = Zmodem ? 03:030;    /* Interrupt char */
  257. #endif
  258.         tty.sg_flags |= (ODDP|EVENP|CBREAK);
  259.         tty.sg_flags &= ~(ALLDELAY|CRMOD|ECHO|LCASE);
  260.         ioctl(0, TIOCSETP, &tty);
  261.         ioctl(0, TIOCSETC, &tch);
  262. #ifdef LLITOUT
  263.         ioctl(0, TIOCLBIS, &Locbit);
  264. #endif
  265.         bibi(99);    /* un-raw doesn't work w/o lit out */
  266.         did0 = TRUE;
  267.         return OK;
  268.     case 1:
  269.     case 3:
  270.         if(!did0) {
  271.             ioctl(0, TIOCEXCL, 0);
  272.             ioctl(0, TIOCGETP, &oldtty);
  273.             ioctl(0, TIOCGETC, &oldtch);
  274. #ifdef LLITOUT
  275.             ioctl(0, TIOCLGET, &Locmode);
  276. #endif
  277.         }
  278.         tty = oldtty;
  279.         tty.sg_flags |= (RAW|TANDEM);
  280.         tty.sg_flags &= ~ECHO;
  281.         ioctl(0, TIOCSETP, &tty);
  282.         did0 = TRUE;
  283.         Effbaud = Baudrate = getspeed(tty.sg_ospeed);
  284.         return OK;
  285. #endif
  286.     case 0:
  287.         if(!did0)
  288.             return ERROR;
  289. #ifdef USG
  290.         (void) ioctl(0, TCSBRK, 1);    /* Wait for output to drain */
  291.         (void) ioctl(0, TCFLSH, 1);    /* Flush input queue */
  292.         (void) ioctl(0, TCSETAW, &oldtty);    /* Restore modes */
  293.         (void) ioctl(0, TCXONC,1);    /* Restart output */
  294. #endif
  295. #ifdef V7
  296.         ioctl(0, TIOCSETP, &oldtty);
  297.         ioctl(0, TIOCSETC, &oldtch);
  298.         ioctl(0, TIOCNXCL, 0);
  299. #ifdef LLITOUT
  300.         ioctl(0, TIOCLSET, &Locmode);
  301. #endif
  302. #endif
  303.  
  304.         return OK;
  305.     default:
  306.         return ERROR;
  307.     }
  308. }
  309.  
  310. sendbrk()
  311. {
  312. #ifdef V7
  313. #ifdef TIOCSBRK
  314. #define CANBREAK
  315.     sleep(1);
  316.     ioctl(0, TIOCSBRK, 0);
  317.     sleep(1);
  318.     ioctl(0, TIOCCBRK, 0);
  319. #endif
  320. #endif
  321. #ifdef USG
  322. #define CANBREAK
  323.     ioctl(0, TCSBRK, 0);
  324. #endif
  325. }
  326.  
  327. /* End of rbsb.c */
  328.